"""
@File    :   redis_operate.py
@Time    :   2024/03/18 16:29:32
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   redis操作
"""

from redis import Redis

from src.config import get_app_settings
from src.enums.redis_db import RedisDbNumEnum

# 获取应用配置
settings = get_app_settings()


class RedisOperate:
    """cli的Redis相关操作类"""

    @staticmethod
    def flush_db(db_num: int):
        """清空redis指定的db

        Args:
            db_num (int): 数据库的db数
        """
        # 创建Redis连接池
        redis_pool = Redis(
            host=settings.redis_host,
            password=settings.redis_password,
            port=settings.redis_port,
            db=db_num,
            decode_responses=True,
        )
        redis_pool.flushdb()
        redis_pool.close()

    @staticmethod
    def flush_all_db():
        """清空redis的所有db

        Args:
            redis_pool (Redis): redis连接池
        """
        # 创建Redis连接池
        redis_pool = Redis(
            host=settings.redis_host,
            password=settings.redis_password,
            port=settings.redis_port,
            db=RedisDbNumEnum.search_db.value,
            decode_responses=True,
        )
        redis_pool.flushall()
        redis_pool.close()
